/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package di.server.security.dao.impl;

import di.persistence.dao.UserDao;
import di.persistence.entities.User;
import di.server.security.Assembler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Kilian Ito
 */
@Service("userDetailsService") 
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserDao dao;
    @Autowired
    private Assembler assembler;

    @Transactional(readOnly = true)
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
        UserDetails userDetails = null;
        User userEntity = dao.getUserByLogin(username);
        if (userEntity == null)
            throw new UsernameNotFoundException("user not found");
        
        return assembler.buildUserFromUserEntity(userEntity);
    }
}
